//
//  YDYNavigationController.m
//  DYSisterAPP
//
//  Created by 杨德勇 on 16/7/26.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYNavigationController.h"

@interface YDYNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation YDYNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    //重写左边的按钮，代理和手势识别会失效
    self.interactivePopGestureRecognizer.delegate = self;
    
    
    //默认导航控制器是透明的，切换视图的时候能看见后面，不好看
//    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    
    //tabbar加载后每个底部按键都需要push 1个界面，但是由于super还没执行，所以count为0，知道方法最后执行super之后，才＋1
    if (self.childViewControllers.count >0) {
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [backBtn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        [backBtn setTitle:@"返回" forState:UIControlStateNormal];
        [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [backBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        [backBtn sizeToFit];
        [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
        
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
        viewController.hidesBottomBarWhenPushed = YES ;
    }
    
    
    [super pushViewController:viewController animated:animated];
}

-(void)back{
    [self popViewControllerAnimated:YES];
}



#pragma mark -
#pragma mark -手势控制器代理
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    
    //如果子控制器为1，说明是根控制器，手势必须失效，不然程序就bug了
    return self.childViewControllers.count > 1;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
